package cn.stylefeng.guns.modular.rec.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.transport.entity.ZhuangyunMingxiTongji;
import cn.stylefeng.guns.modular.transport.model.params.TransportDispatchParam;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/rec/carrierRecFeeCalc")
public class CarrierRecFeeCalcController {

    private String PREFIX = "/rec/carrierRecFeeCalc";

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private TransportDispatchService transportDispatchService;

    @Resource
    private RoleService roleService;

    @Autowired
    private BasicCarrierService basicCarrierService;



    /**
     * 跳转到 好想来TMS监控
     */
    @RequestMapping("")
    public String hx_totmsmonitor(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/carrierRecFeeCalc.html";
    }


    /**
     * 跳转修改页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/carrierRecFeeCalc_edit.html";
    }



    /**
     * 查询分页
     * @param transportDispatchParam
     * @param condition
     * @param startTime
     * @param endTime
     * @param operateId
     * @return
     */
    @ResponseBody
    @RequestMapping("/listPage")
    public LayuiPageInfo listPage(TransportDispatchParam transportDispatchParam, String condition, String startTime, String endTime
            , Long operateId) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if(bySpec != null){
                        transportDispatchParam.setCarrierId(Long.valueOf(bySpec.getId()));
                    }else{
                        transportDispatchParam.setCarrierName(user.getName());
                    }
                }
            }
        }
        transportDispatchParam.setDeptId(user.getDeptId());
        transportDispatchParam.setDeliverId(user.getDeliverId());
        String times = "";
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            times = "'" + startTime + "' and '" + endTime + "'";
        } else if (startTime == null && endTime == null) {
            startTime = DateUtil.getDate();
            endTime = DateUtil.getDate();
            times = "'" + startTime + "' and '" + endTime + "'";
        }
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(operateId)){
            operateId = user.getOperateId();
        }
        return this.transportDispatchService.findCarrierRecFeePage(transportDispatchParam, condition, times, operateId, user.getOperateId());
    }


    /**
     * 查询全部
     * @param transportDispatchParam
     * @param condition
     * @param startTime
     * @param endTime
     * @param operateId
     * @return
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<ZhuangyunMingxiTongji> listAll(TransportDispatchParam transportDispatchParam, String condition, String startTime, String endTime
            , Long operateId) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("承运商")){
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if(bySpec != null){
                        transportDispatchParam.setCarrierId(Long.valueOf(bySpec.getId()));
                    }else{
                        transportDispatchParam.setCarrierName(user.getName());
                    }
                }
            }
        }
        transportDispatchParam.setDeptId(user.getDeptId());
        transportDispatchParam.setDeliverId(user.getDeliverId());
        String times = "";
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            times = "'" + startTime + "' and '" + endTime + "'";
        } else if (startTime == null && endTime == null) {
            startTime = DateUtil.getDate();
            endTime = DateUtil.getDate();
            times = "'" + startTime + "' and '" + endTime + "'";
        }
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(operateId)){
            operateId = user.getOperateId();
        }
        return this.transportDispatchService.findCarrierRecFeeAll(transportDispatchParam, condition, times, operateId, user.getOperateId());
    }

    /**
     * 计算运费
     * @param ids
     * @return
     */
    @RequestMapping("/jsfy")
    @ResponseBody
    public ResponseData jsfy(@RequestParam("ids") String ids) {
        return this.transportDispatchService.jsfy(ids);
    }

}
